import Vue from 'vue';
import App from './App.vue';
import router from './router';
import axios from 'axios';
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import 'font-awesome/css/font-awesome.css'

import {postRequest} from "./utils/api";
import {getRequest} from "./utils/api";
import {putRequest} from "./utils/api";
import {deleteRequest} from "./utils/api";
import {initMenu} from "./utils/menus";
import {downloadRequest} from "./utils/download";

Vue.config.productionTip = false;
Vue.prototype.$axios = axios;
Vue.prototype.postRequest = postRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.deleteRequest = deleteRequest;
Vue.prototype.downloadRequest = downloadRequest;
Vue.use(ElementUI, {size:'small'});

// 全局前置守卫
// to: Route: 即将要进入的目标 路由对象
// from: Route: 当前导航正要离开的路由
// next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。
router.beforeEach(((to, from, next) => {
  if (window.sessionStorage.getItem('tokenStr')) {
    initMenu(router, store);
    //判断用户信息是否存在
    if (!window.sessionStorage.getItem('user')) {
      return postRequest('/admin/info').then(resp => {
        if (resp) {
          //存入用户信息
          let userinfo = resp.obj;
          window.sessionStorage.setItem('user', JSON.stringify(userinfo));
          store.commit('INIT_ADMIN', resp);
          next();
        }
      });
    }
    next();
  } else {
    //若未登录 跳转登录页
    if (to.path == '/') {
      next();
    } else {
      //若直接访问的是某个页面，则重定向页面
      next('/?redirect='+to.path);
    }
  }
}));

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
